/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author cuong
 */
public class ConnectDB {

    private Connection con;
    private Statement st;
    private ResultSet rs;
    private String url;
    private String serverName;
    private String databaseName;
    private String instanceName;
    private String userName;
    private String password;
    private String sql;

    public Statement ConnectionDB() {
        con = this.getConnection();
        try {
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return st;
    }

    private String getConnectionUrl() {
        url = "jdbc:sqlserver://";
        serverName = "localhost:1433";
        databaseName = "BluePumpkinDB";
        instanceName = "CUONGNTK\\CUONGNTK";
        userName = "sa";
        password = "1";
        return url + serverName + ";instanceName=" + instanceName +
                ";databaseName=" + databaseName + ";";
    }

    private Connection getConnection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(getConnectionUrl(),
                    userName, password);
        }catch (SQLException ex) {
            ex.printStackTrace();
        }catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        if (con != null)
            return con;
        else
            return null;
    }
}
